home *** CD-ROM | disk | FTP | other *** search
/ WINMX Assorted Textfiles / Ebooks.tar / Text - Tech - Programming - Visual Basic - Nod Programing VB Help Index 4 (TXT).zip / VBTips4.txt
Text File  |  1998-04-09  |  11KB  |  333 lines

  1.  
  2.                Nod Programming Inc. VB Help Index
  3. »»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»
  4. This is intended for free use.  The code here is for various skill levels, 
  5. anyone from beginers to advanced programers can use these.  Do what you wish 
  6. with the code it is free for you to use and manipulate!
  7.  
  8. ****************************************************************************
  9.  
  10. Creating Win32 region windows:
  11.  
  12. The Win32 API includes a really amazing feature called region windows. A
  13. window under Win32 no longer has to be rectangular! In fact, it can be any
  14. shape that may be constructed using Win32 region functions. Using the
  15. SetWindowRgn Win32 function from within VB is so simple, but the results
  16. are unbelievable! The following example shows a VB form that is NOT
  17. rectangular!!
  18.  
  19. Here is the code. Enjoy!
  20.  
  21.  ' This goes into the General Declarations section:
  22.  
  23. Private Declare Function CreateEllipticRgn Lib "gdi32" _
  24.  (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, _
  25.  ByVal Y2 As Long) As Long
  26.  
  27. Private Declare Function SetWindowRgn Lib "user32" _
  28.  (ByVal hWnd As Long, ByVal hRgn As Long, _
  29.  ByVal bRedraw As Boolean) As Long
  30.  
  31.  
  32. Private Sub Form_Load()
  33.  
  34. Show 'The form!
  35. SetWindowRgn hWnd, _
  36.   CreateEllipticRgn(0, 0, 300, 200), _
  37.   True
  38.  
  39. End Sub
  40.  
  41. ****************************************************************************
  42.  
  43. Manipulate your controls from the keyboard:
  44.  
  45. If you're not comfortable using your mouse--or can't achieve the precise
  46. results you'd like--these tips will come in handy.
  47.  
  48. First, you can resize controls at design time by using the [Shift] and
  49. arrow keys, as follows:
  50.  
  51.      SHIFT + RIGHT ARROW increases the width of the control
  52.      SHIFT + LEFT ARROW decreases the width of the control
  53.      SHIFT + DOWN ARROW increases the height of the control
  54.      SHIFT + UP ARROW decreases the height of the control
  55.  
  56. Note: The target control must have focus, so click on the control before
  57. manipulating it from the keyboard.
  58.  
  59. Second, by using the [Control] key and the arrow keys, you can move your
  60. controls at design time, as follows:
  61.  
  62.      CONTROL + RIGHT ARROW to move the control to the right
  63.      CONTROL + LEFT ARROW to move the control to the left
  64.      CONTROL + DOWN ARROW to move the control downwards
  65.      CONTROL + UP ARROW to move the control upwards
  66.  
  67. If you select more than one control (by clicking on the first and
  68. shift-clicking on the others), the above procedures will affect all the
  69. selected controls.
  70.  
  71. ****************************************************************************
  72.  
  73. Simple file checking from anywhere:
  74.  
  75. To keep my applications running smoothly, I often need to check that
  76. certain files exist.  So, I've written a simple routine to make sure they
  77. do. Here it is:
  78.  
  79. Public Sub VerifyFile(FileName As String)
  80.     '
  81.     On Error Resume Next
  82.     'Open a specified existing file
  83.     Open FileName For Input As #1
  84.     'Error handler generates error message with file and exits the routine
  85.     If Err Then
  86.         MsgBox ("The file " & FileName & " cannot be found.")
  87.         Exit Sub
  88.     End If
  89.     Close #1
  90.     '
  91. End Sub
  92.  
  93. Now add a button to your form and place the code below behind the "Click"
  94. event.
  95.  
  96. Private Sub cmdVerify_Click()
  97.     '
  98.     Call VerifyFile("MyFile.txt")
  99.     '
  100. End Sub
  101.  
  102. ****************************************************************************
  103.  
  104. Dragging items from one list to another:
  105.  
  106. Here's a way that you can let users drag items from one list and drop them
  107. in another one.
  108.  
  109. Create two lists (lstDraggedItems, lstDroppedItems) and a text box
  110. (txtItem) in a form (frmTip).
  111.  
  112. Put the following code in the load event of your form.
  113.  
  114. Private Sub Form_Load()
  115.     ' Set the visible property of txtItem to false
  116.     txtItem.Visible = False
  117.     'Add items to list1 (lstDraggedItems)
  118.     lstDraggedItems.AddItem "Apple"
  119.     lstDraggedItems.AddItem "Orange"
  120.     lstDraggedItems.AddItem "Grape"
  121.     lstDraggedItems.AddItem "Banana"
  122.     lstDraggedItems.AddItem "Lemon"
  123.     '
  124. End Sub
  125.  
  126. In the mouseDown event of  the list lstDraggedItems put the following code:
  127.  
  128. Private Sub lstDraggedItems_MouseDown(Button As Integer, Shift As Integer,
  129. X As Single, Y As Single)
  130.     '
  131.     txtItem.Text = lstDraggedItems.Text
  132.     txtItem.Top = Y + lstDraggedItems.Top
  133.     txtItem.Left = X + lstDraggedItems.Left
  134.     txtItem.Drag
  135.     '
  136. End Sub
  137.  
  138. In the dragDrop event of the list lstDroppedItems put the following code:
  139.  
  140. Private Sub lstDroppedItems_DragDrop(Source As Control, X As Single, Y As
  141. Single)
  142.     '
  143.     If lstDraggedItems.ItemData(lstDraggedItems.ListIndex) = 9 Then
  144.         Exit Sub
  145.     End If
  146.     ' To make sure that this item will not be selected again
  147.     lstDraggedItems.ItemData(lstDraggedItems.ListIndex) = 9
  148.     lstDroppedItems.AddItem txtItem.Text
  149.     '
  150. End Sub
  151.  
  152. Now you can drag items from lstDraggedItems and drop them in=
  153.  LstDroppedItems.
  154.  
  155. Note that you cannot drag from the second list to the first. Also, the
  156. dragged item remains in the first list. You'll have to address those
  157. limitations yourself.
  158.  
  159. ****************************************************************************
  160.  
  161. Creating a new context menu in editable controls:
  162.  
  163. This routine will permit you to replace the original context menu with your
  164. private context menu in an editable control.
  165.  
  166. Add the following code to your form or to a BAS module:
  167.  
  168. Private Const WM_RBUTTONDOWN = &H204
  169. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
  170. (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As
  171. Any) As Long
  172.  
  173. Public Sub OpenContextMenu(FormName As Form, MenuName As Menu)
  174.        
  175.     'Tell system we did a right-click on the mdi
  176.     Call SendMessage(FormName.hwnd, WM_RBUTTONDOWN, 0, 0&)
  177.     'Show my context menu
  178.     FormName.PopupMenu MenuName
  179.     '
  180. End Sub
  181.  
  182. Next, use the Visual Basic Menu Editor and the table below to create a
  183. simple menu.
  184.  
  185. Caption        Name        Visible
  186. Context Menu    mnuContext    NO
  187. ...First Item    mnuContext1
  188. ...Second Item    mnuContext2
  189.  
  190. Note that the last two items in the menu are indented (...) one level and
  191. that only the first item in the list ("Context Menu") has the Visible
  192. property set to NO.
  193.  
  194. Now add a text box to your form and enter the code below in the MouseDown
  195. event of the text box.
  196.  
  197. Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As
  198. Single, Y As Single)
  199.     If Button = vbRightButton Then
  200.         Call OpenContextMenu(Me, Me.mnuContext)
  201.     End If
  202. End Sub
  203.  
  204. Note: If you just want to kill the system context menu, just comment out
  205. the line:
  206.  
  207.     FormName.PopupMenu MenuName
  208.  
  209. in the OpenContextMenu routine.
  210.  
  211. ****************************************************************************
  212.  
  213. Quick Custom Dialogs for DBGrid Cells:
  214.  
  215. It's easy to add custom input dialogs to al the cells in the Microsoft Data
  216. Bound Grid control.
  217.  
  218. First, add a DBGrid control and Data control to your form. Next, set the
  219. DatabaseName and RecordSource properties of the data control to a valid
  220. database and table ("biblio.mdb" and "Publishers" for example). Then set
  221. the DataSource property of the DBGrid control to Data1 (the data control).
  222.  
  223. Now add the following code to your form.
  224.  
  225. ' general declaration area
  226. Dim strDBGridCell As String
  227.  
  228.  
  229. Private Sub DBGrid1_AfterColEdit(ByVal ColIndex As Integer)
  230.     '
  231.     DBGrid1.Columns(ColIndex) = strDBGridCell
  232.     '
  233. End Sub
  234.  
  235. Private Sub DBGrid1_BeforeColEdit(ByVal ColIndex As Integer, ByVal KeyAscii
  236. As Integer, Cancel As Integer)
  237.     '
  238.     strDBGridCell = InputBox("Edit DBGrid Cell:", ,=
  239.  DBGrid1.Columns(ColIndex))
  240.     '
  241. End Sub
  242.  
  243. Now whenever you attempt to edit any cell in the DBGrid, you'll see the
  244. InputBox prompt you for input.  You can replace the InputBox with any other
  245. custom dialog you wish to build.
  246.  
  247. ****************************************************************************
  248.  
  249. Using the Alias Option to Prevent API Crashes:
  250.  
  251. A number of Windows APIs have parameters that can be more than one data
  252. type. For example, the WinHelp API call can accept the last parameter as a
  253. Long or String data type depending on the service requested.
  254.  
  255. Visual Basic allows you to declare this data type as "Any" in the API call,
  256. but this can lead to type mismatch errors or even system crashes if the
  257. value is not the proper form.
  258.  
  259. You can prevent the errors and improve the run-time type checking by
  260. declaring multiple versions of the same API function in your program. By
  261. adding a function declaration for each possible parameter type, you can
  262. continue to use strong data type checking.
  263.  
  264. To illustrate this technique, add the following APIs and constants to a
  265. Visual Basic form. Notice that the two API declarations differ only in
  266. their initial name ("WinHelp" and "WinHelpSearch") and the type declaration
  267. of the last parameter ("dwData as Long" and "dwData as String").
  268.  
  269. ' WinHelp APIs
  270. Private Declare Function WinHelp Lib "user32" Alias "WinHelpA" (ByVal hwnd
  271. As Long, ByVal lpHelpFile As String, ByVal wCommand As Long, ByVal dwData
  272. As Long) As Long
  273. Private Declare Function WinHelpSearch Lib "user32" Alias "WinHelpA" (ByVal
  274. hwnd As Long, ByVal lpHelpFile As String, ByVal wCommand As Long, ByVal
  275. dwData As String) As Long
  276. '
  277. Private Const HELP_PARTIALKEY = &H105&
  278. Private Const HELP_HELPONHELP = &H4
  279. Private Const HelpFile = "c:\program files\devstudio\vb5\help\vb5.hlp"
  280.  
  281. Now add two command buttons to your form (cmdHelpAbout and cmdHelpSearch)
  282. and place the following code behind the buttons. Be sure to edit the
  283. location of the help file to match your installation of Visual Basic.
  284.  
  285. Private Sub cmdHelpAbout_Click()
  286.     '
  287.     WinHelp Me.hwnd, HelpFile, HELP_HELPONHELP, &H0
  288.     '
  289. End Sub
  290.  
  291. Private Sub cmdHelpSearch_Click()
  292.     '
  293.     WinHelpSearch Me.hwnd, HelpFile, HELP_PARTIALKEY, "option"
  294.     '
  295. End Sub
  296.  
  297. When you press on the HelpAbout button, you'll see help about using the
  298. help system. When you press on the HelpSearch button, you'll see a list of
  299. help entries on the "option" topic.
  300.  
  301. ****************************************************************************
  302.  
  303. Add Dithered Backgrounds to your VB Forms:
  304.  
  305. Ever wonder how the SETUP.EXE screen gets its cool shaded background
  306. coloring? This color shading is called dithering, and you can easily
  307. incorporate it into your forms. Add the following routine to a form:
  308.  
  309.      Sub Dither(vForm As Form)
  310.      Dim intLoop As Integer
  311.          vForm.DrawStyle = vbInsideSolid
  312.          vForm.DrawMode = vbCopyPen
  313.          vForm.ScaleMode = vbPixels
  314.          vForm.DrawWidth = 2
  315.          vForm.ScaleHeight = 256
  316.          For intLoop = 0 To 255
  317.            vForm.Line (0, intLoop)-(Screen.Width, intLoop - 1), RGB(0, 0,
  318. 255 -intLoop), B
  319.          Next intLoop
  320.      End Sub
  321.  
  322. Now, add to the Form_Activate event the line
  323.  
  324.      Dither ME
  325.  
  326. This version creates a fading blue background by adjusting the blue value
  327. in the RGB function. (RGB stands for Red-Green-Blue.) You can create a
  328. fading red background by changing the RGB call to
  329.  
  330.      RGB(255 - intLoop, 0, 0).
  331.  
  332. ****************************************************************************
  333.                    End of Help 4 of how many I do!!